<?php
require_once('inc/config.inc.php');
$XMLstr = "";
$mainversion	= $_GET['mainversion'];
$updateversion	= $_GET['updateversion'];
$provider	= $_GET['provider'];
if ($mainversion != "" && $provider != "")
{
	$XMLstr .="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	$XMLstr .="<Information>\n";
	$sql = "SELECT f.fid,p.pname,f.oldversion,f.newversion,f.mode FROM kkyoo_query.q_serveprovider as p,kkyoo_query.q_fileupdate as f WHERE p.pid = f.pid AND p.pname='".$provider."'";
	if($result = $conn->Execute($sql)){
		while($arrRow = $result->FetchRow())
		{
			if ($arrRow['fid'] == "1")
			{
				$provider = $arrRow['pname'];
				if ($arrRow['newversion'] > $updateversion){$U_status = 'yes';$updateversion = $arrRow['newversion'];}
				else{$U_status = 'no';}
				$XMLstr .="<updateinfo version=\"$updateversion\" update=\"$U_status\" privder=\"$provider\"/>\n";
			}
			elseif($arrRow['fid'] == "2")
			{
				//$mode = $arrRow['mode'];
				if ($mainversion > $arrRow['oldversion']){$mode = 'backend';} else {$mode = 'frontend';}
				if ($arrRow['newversion'] > $mainversion){$M_status = 'yes';$mainversion = $arrRow['newversion'];}
				else{$M_status = 'no';}
				$XMLstr .="<maininfo version=\"$mainversion\" update=\"$M_status\" mode=\"$mode\"/>\n";
			}
		}
	}
	$sql = "SELECT p.pid,f.md5check,f.path,f.fid FROM kkyoo_query.q_serveprovider as p,kkyoo_query.q_fileupdate as f WHERE p.pid = f.pid AND p.pname='".$provider."'";
	if($result = $conn->Execute($sql)){
		while($arrRow = $result->FetchRow())
		{
			$md5check = $arrRow['md5check'];
			$path = $arrRow['path'];
			if ($arrRow['fid'] == "1" && $U_status == 'yes')
			{
				$XMLstr .="<updatefile md5=\"$md5check\">\n";
				$sql = "SELECT addrurl FROM kkyoo_query.q_updateaddr WHERE pid=".$arrRow['pid']." AND fid=".$arrRow['fid'];
				if($result1 = $conn->Execute($sql)){
					while($arrRow1 = $result1->FetchRow()){$XMLstr .="<url addr=\"".$arrRow1['addrurl'].$path."\"/>\n";}
				}
				$XMLstr .="</updatefile>\n";
			}
			elseif ($arrRow['fid'] == "2" && $M_status == 'yes')
			{
				$XMLstr .="<mainfile md5=\"$md5check\">\n";
				$sql = "SELECT addrurl FROM kkyoo_query.q_updateaddr WHERE pid=".$arrRow['pid']." AND fid=".$arrRow['fid'];
				if($result1 = $conn->Execute($sql)){
					while($arrRow1 = $result1->FetchRow()){$XMLstr .="<url addr=\"".$arrRow1['addrurl'].$path."\"/>\n";}
				}
				$XMLstr .="</mainfile>\n";
			}
		}
	}
	$XMLstr .="</Information>\n";
	$host = $_SERVER['REMOTE_ADDR'];
	$sql = 'CALL kkyoo_query.sp_server_updatecount("'.$provider.'","'.$mainversion.'","'.$host.'")';
	$conn->Execute($sql);
}
echo $XMLstr;
exit;